package com.Action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.Bean.rolemenuModel;
import com.Bean.wdmenuModel;
import com.Bean.wdroleModel;
import com.Service.wdroleService;
import com.Service.rolemenuService;
import com.Service.wdmenuService;
@Controller
@RequestMapping("/role")/**分路径****/
public class wdroleAction {
	
	@Autowired
	private wdroleService wdroleService;
	@Autowired
	private wdmenuService  wdmenuService;
	@Autowired
	private rolemenuService  rolemenuService;
	
	@RequestMapping("/selectrole.do")
	public @ResponseBody Map<String, String> toAjax(Integer yanz){
	 System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!in toAjax wdroleAction");
	  List<wdroleModel>  rlist = wdroleService.selectchukai(yanz);
	  wdroleModel  wdrole=wdroleService.selectByPrimaryKey(yanz);
	  Map<String, String> comMap = new HashMap<String, String>();
	   String  gg="";
		for(wdroleModel wdd:rlist){
             gg=gg+ "<option value='"+wdd.getRoleId()+"'"+">"+wdd.getRoleName()+"</option>";  			
		}	
		System.out.println(" gg的字符"+gg);
	  /*<option value="1">销售员</option>*/
	  String fhui="<option value='"+wdrole.getRoleId()+"'"+"selected='selected'>"+wdrole.getRoleName()+"</option>"+gg;
	  System.out.println(" fhui的字符"+fhui);
	  comMap.put("complexList", fhui);
	  return comMap;
	}
	
	@RequestMapping("/selectrole2.do")
	public @ResponseBody Map<String, String> toAjaxyanz(Integer yanz){
		 System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!in toAjaxyanz wdroleAction");
		    
		 List<wdroleModel> listw=wdroleService.selectAll();
		  Map<String, String> comMap = new HashMap<String, String>();
		   String  gg="";
			for(wdroleModel wdd:listw){
				 gg=gg+ "<option value='"+wdd.getRoleId()+"'"+">"+wdd.getRoleName()+"</option>"; 
				
			}
			System.out.println(" gg的字符"+gg);
			 String fhui="<option value=0"+"  selected='selected'>"+"请选择角色"+"</option>"+gg;
			comMap.put("complexList", fhui);
			return comMap;
	}
	
	
	
	@RequestMapping("/selectqauanxian.do")
	  public String deletUserQX(String userID,Model model,HttpServletRequest request){
		List<rolemenuModel> list=rolemenuService.selectByUserID(userID);
		List<wdmenuModel>  wlist=new ArrayList<wdmenuModel>();
		request.setAttribute("userID",userID);
		if(list.size()>0){
			for(rolemenuModel role:list){
				wdmenuModel	wdmen=wdmenuService.selectByPrimaryKey(role.getMenuId());
				wlist.add(wdmen);
			}
		}	
		model.addAttribute("wlist", wlist);
		return "quanxiandele";
	}
	
	
	@RequestMapping("/selectqauanxian2.do")
	  public String selectUserQX(String userID,Model model,HttpServletRequest request){
		List<rolemenuModel> list=rolemenuService.selectByUserID(userID);
		List<wdmenuModel>  wlist=new ArrayList<wdmenuModel>();
		List<wdmenuModel>  wlist2=new ArrayList<wdmenuModel>();
		List<wdmenuModel>  wAlllist=wdmenuService.selectAll();
		request.setAttribute("userID",userID);
		if(list.size()>0){
			
			for(rolemenuModel role:list){
				wdmenuModel	wdmen=wdmenuService.selectByPrimaryKey(role.getMenuId());
				wlist.add(wdmen);
			}
			
			
			for(wdmenuModel wd2:wlist){
				System.out.println("###################"+wd2.getMenuId()+"   ");
			}
			
			
			
			for(wdmenuModel wd1:wAlllist){
				Boolean  flag=false;
				for(wdmenuModel wd2:wlist){
					if(wd1.getMenuId().trim().equals(wd2.getMenuId().trim())){
						 flag=true;
						 System.out.println("有的权限id"+wd1.getMenuId()+" ");
					}
					
				}
				if(flag!=true){
					System.out.println(" 没有的权限id"+wd1.getMenuId()+" ");
					wlist2.add(wd1);
				}
				
			}
			
			model.addAttribute("wlist2", wlist2);
		}else{
			model.addAttribute("wlist2", wAlllist);
			
		}
		for(wdmenuModel wd2:wlist2){
			System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"+wd2.getMenuId()+"   ");
		}
		
		
		
		return "quanxianfuyu";
	}
	
	@RequestMapping("/toaddQX.do")
	 public String addUserQX(String list1,String userID){
		System.out.println("in  addUserQX"+list1+"  "+userID);
		String[] sourceStrArray=list1.split(",");
		for(int i = 0; i < sourceStrArray.length; i++){
			 System.out.println(sourceStrArray[i]+"");
			 rolemenuModel record=new rolemenuModel();
			 record.setUserId(userID);
			 record.setMenuId(sourceStrArray[i]);
			int a=rolemenuService.insertSelective(record);
			System.out.println("添加权限 的 a"+a);
			if(a>0){
			String pid=wdmenuService.selectByPrimaryKey(sourceStrArray[i]).getPid();
			System.out.println("wdmenuService.selectByPrimaryKey(pid)"+pid);
			 rolemenuModel record3=new rolemenuModel();
			 record3.setUserId(userID);
			 record3.setMenuId(pid);
			 System.out.println("添加全新！！！！！！！！！！"+record3.getMenuId()+" "+record3.getUserId());
			if(rolemenuService.selectcongfu(record3)==null){
				 rolemenuModel record2=new rolemenuModel();
				 record2.setMenuId(pid);
				 record2.setUserId(userID);
				 int aa=rolemenuService.insertSelective(record2);
				 System.out.println("添加权限#########"+record2.getMenuId()+"  "+record2.getUserId());
				 System.out.println("添加权限##############3"+aa);
			}
			}
			 
			
		}
		return "redirect:/user/quanxian.do";
		
	}
	
	@RequestMapping("/todelQX.do")
	public String delUserQX(String list1,String userID){
		System.out.println("in  delUserQX"+list1+"  "+userID);
		String[] sourceStrArray=list1.split(",");
		for(int i = 0; i < sourceStrArray.length; i++){
			 System.out.println(sourceStrArray[i]+"");
			 rolemenuModel record=new rolemenuModel();
			 record.setUserId(userID);
			 record.setMenuId(sourceStrArray[i]);
		int a=rolemenuService.deleteByuser(record);
		
		if(a>0){
			String pid=wdmenuService.selectByPrimaryKey(sourceStrArray[i]).getPid();
			 List<wdmenuModel> list3=wdmenuService.selectChild(pid);
			 List<rolemenuModel> list4=rolemenuService.selectByUserID(userID);
			 Boolean  flagg=false;
			 for(wdmenuModel wd:list3){
				 System.out.println("@@@@@@@@@@删除权限"+wd.getMenuId());
				
				 for(rolemenuModel rolem:list4){
					    System.out.println("list4的值"+rolem.getMenuId()+" ");
					    if(wd.getMenuId().equals(rolem.getMenuId())){
					    	
					    	flagg=true;
					    	/* rolemenuModel record2=new rolemenuModel();
							 record.setUserId(userID);
						 record.setMenuId(wd.getMenuId());
					  int aa=rolemenuService.deleteByuser(record2);
						  System.out.println("删除权限 "+aa);*/
					    }
					    
				 }
				
					 
			 }if(flagg==false){
				 rolemenuModel record2=new rolemenuModel();
				 record2.setUserId(userID);
			     record2.setMenuId(pid);
			     System.out.println("删除权限 record2的值"+record2.getUserId()+record2.getMenuId());
		         int aa=rolemenuService.deleteByuser(record2);
			     System.out.println("删除权限 "+aa);
				 
			 }
			
		}
		
		
		}
		return "redirect:/user/quanxian.do";
	}
	

}
